package com.example.communication;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button type1;
	private Button type2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initView();
		initHandler();
	}

	private void initHandler() {
		// TODO Auto-generated method stub
		type1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i ("ACT_COMMU ","Type1");
				Intent it = new Intent();
				it.setClass(MainActivity.this,Activity22.class );
				
				Bundle b = new Bundle();
				b.putString("NAME", "Ken Tai");
				b.putInt("AGE", 40);
				it.putExtras(b);
				startActivity(it);
				finish();
			}
		});
		type2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i ("ACT_COMMU ","Type1");
				Intent it = new Intent();
				it.setClass(MainActivity.this,MainActivity3.class );
				startActivityForResult(it,0);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 0){
			String message = data.getExtras().getString("RESULT");
			Log.i("ACT_COMMU", "MESSAGE : "+message);
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
			
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		type1 = (Button)this.findViewById(R.id.button1);
		type2 = (Button)this.findViewById(R.id.button2);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {;
	
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
